<template>
  <div class="operateLog">
    <div class="operateLog-content">
      <div class="operateLog-content-title">
        <div class="operateLog-content-title-left">操作日志查询</div>
        <div class="operateLog-content-title-right">
          <el-button icon="el-icon-download" @click="handleExport">
            导出日志
          </el-button>
          <el-button type="primary" icon="el-icon-refresh-right" @click="getList">刷新数据</el-button>
        </div>
      </div>
      <div class="operateLog-content-search">
        <div class="operateLog-content-search-title">筛选条件</div>
        <div class="operateLog-content-search-content">
          <el-form style="flex: 1" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-position="top" label-width="68px">
            <el-row style="position: relative;">
              <el-col :span="4"><el-form-item label="学校" prop="businessType">
                <el-select v-model="queryParams.schoolId" placeholder="请选择学校" clearable>
                  <el-option
                    v-for="item in schoolList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item></el-col>
              <el-col :span="4">
                <el-form-item label="操作人员" prop="operName">
                  <el-input
                    v-model="queryParams.operName"
                    placeholder="请输入操作人员"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item label="操作类型" prop="businessType">
                  <el-select v-model="queryParams.businessType" placeholder="请选择渠道" clearable>
                    <el-option
                      v-for="dict in dict.type.sys_business_type"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="4"><el-form-item label="渠道" prop="operatorType">
                <el-select v-model="queryParams.operatorType" placeholder="请选择操作类型" clearable>
                  <el-option
                    v-for="dict in dict.type.sys_operator_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item></el-col>
              <el-col :span="5"><el-form-item label="时间范围" prop="subjectType">
                <el-date-picker
                  v-model="dateRange"
                  style="width: 100%"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  type="datetimerange"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :default-time="['00:00:00', '23:59:59']"
                ></el-date-picker>
              </el-form-item></el-col>
            </el-row>
            <el-row>
              <div style="display:flex;align-items:center;margin-top: 20px;">
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </div>
            </el-row>
          </el-form>
        </div>

      </div>


<!--      <el-row :gutter="10" class="mb8">-->
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--            type="primary"-->
<!--            plain-->
<!--            icon="el-icon-plus"-->
<!--            size="mini"-->
<!--            @click="handleAdd"-->
<!--            v-hasPermi="['classai:resource:add']"-->
<!--          >新增</el-button>-->
<!--        </el-col>-->
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--            type="success"-->
<!--            plain-->
<!--            icon="el-icon-edit"-->
<!--            size="mini"-->
<!--            :disabled="single"-->
<!--            @click="handleUpdate"-->
<!--            v-hasPermi="['classai:resource:edit']"-->
<!--          >修改</el-button>-->
<!--        </el-col>-->
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--            type="danger"-->
<!--            plain-->
<!--            icon="el-icon-delete"-->
<!--            size="mini"-->
<!--            :disabled="multiple"-->
<!--            @click="handleDelete"-->
<!--            v-hasPermi="['classai:resource:remove']"-->
<!--          >删除</el-button>-->
<!--        </el-col>-->
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--            type="warning"-->
<!--            plain-->
<!--            icon="el-icon-download"-->
<!--            size="mini"-->
<!--            @click="handleExport"-->
<!--            v-hasPermi="['classai:resource:export']"-->
<!--          >导出</el-button>-->
<!--        </el-col>-->
<!--        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--      </el-row>-->

      <el-table v-loading="loading" :data="logList" @selection-change="handleSelectionChange">
<!--        <el-table-column type="selection" width="55" align="center" />-->
        <el-table-column type="index" width="50" label="序号" align="center">
        </el-table-column>
        <el-table-column label="操作时间" align="center" prop="operTime" />
        <el-table-column label="学校" align="center" prop="schoolName" />
        <el-table-column label="操作人员" align="center" prop="operName" />
<!--        <el-table-column label="操作人员" align="center" prop="resourceImg" >-->
<!--          <template slot-scope="scope">-->
<!--            <image-preview :src="scope.row.resourceImg" :width="50" :height="50"/>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="渠道" align="center" prop="operatorType"  >
          <template slot-scope="scope" >
            <dict-tag :options="dict.type.sys_operator_type" :value="scope.row.operatorType"/>
          </template>
        </el-table-column>
        <el-table-column label="交易类型" align="center" prop="title" />
        <el-table-column label="操作类型" align="center" prop="businessType">
          <template slot-scope="scope">
            <div style="margin:auto;display: flex;align-items: center;" class="status-button" :class="scope.row.businessType ==3 ? 'status-color-fail':scope.row.businessType ==2 ? 'status-color-modify':scope.row.businessType ==1 ? 'status-color-add':scope.row.businessType ==0 ? 'status-color-import':''">
              <dict-tag :options="dict.type.sys_business_type" :value="scope.row.businessType"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleDetails(scope.row)"
              v-hasPermi="['classai:resource:edit']"
            >详情</el-button>
<!--            <el-button-->
<!--              size="mini"-->
<!--              type="text"-->
<!--              icon="el-icon-delete"-->
<!--              @click="handleDelete(scope.row)"-->
<!--              v-hasPermi="['classai:resource:remove']"-->
<!--            >删除</el-button>-->
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>


    <!-- 操作日志详情 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body >
      <div class="dialog-content" style="max-height: 680px;overflow-y: auto">
        <div class="dialog-content-BasicInfo">
          <div class="dialog-content-BasicInfo-title">基本信息</div>
          <div class="dialog-content-BasicInfo-content">
            <div class="dialog-content-BasicInfo-content-row">
              <div class="dialog-content-BasicInfo-content-row-li">
                <div class="dialog-content-BasicInfo-content-row-li-top">操作人员</div>
                <div class="dialog-content-BasicInfo-content-row-li-bottom">{{form.operName}}</div>
              </div>
              <div class="dialog-content-BasicInfo-content-row-li">
                <div class="dialog-content-BasicInfo-content-row-li-top">操作时间</div>
                <div class="dialog-content-BasicInfo-content-row-li-bottom">{{form.operTime}}</div>
              </div>
            </div>
            <div class="dialog-content-BasicInfo-content-row">
              <div class="dialog-content-BasicInfo-content-row-li">
                <div class="dialog-content-BasicInfo-content-row-li-top">操作类型</div>
                <div class="dialog-content-BasicInfo-content-row-li-bottom"> <dict-tag :options="dict.type.sys_business_type" :value="form.businessType"/></div>
              </div>
              <div class="dialog-content-BasicInfo-content-row-li">
                <div class="dialog-content-BasicInfo-content-row-li-top">交易类型</div>
                <div class="dialog-content-BasicInfo-content-row-li-bottom">{{ form.title }}</div>
              </div>
            </div>
            <div class="dialog-content-BasicInfo-content-row">
<!--              <div class="dialog-content-BasicInfo-content-row-li">-->
<!--                <div class="dialog-content-BasicInfo-content-row-li-top">所属学校</div>-->
<!--                <div class="dialog-content-BasicInfo-content-row-li-bottom">系统操作</div>-->
<!--              </div>-->
              <div class="dialog-content-BasicInfo-content-row-li">
                <div class="dialog-content-BasicInfo-content-row-li-top">渠道</div>
                <div class="dialog-content-BasicInfo-content-row-li-bottom"><dict-tag :options="dict.type.sys_operator_type" :value="form.operatorType"/></div>
              </div>
              <div class="dialog-content-BasicInfo-content-row-li">
                <div class="dialog-content-BasicInfo-content-row-li-top">IP地址</div>
                <div class="dialog-content-BasicInfo-content-row-li-bottom">{{ form.operIp }}</div>
              </div>
            </div>

          </div>
        </div>
        <div class="dialog-content-BasicInfo">
          <div class="dialog-content-BasicInfo-title">操作信息</div>
          <div class="dialog-content-BasicInfo-content">
            <div class="dialog-content-BasicInfo-content-row">
              <div class="dialog-content-BasicInfo-content-row-li">
                <div class="dialog-content-BasicInfo-content-row-li-top">交易状态</div>
                <div class="dialog-content-BasicInfo-content-row-li-bottom">
                  {{form.status == 1 ? '失败':form.status == 0 ? '成功':''}}
                </div>
              </div>
            </div>
            <div class="dialog-content-BasicInfo-content-row">
              <div class="dialog-content-BasicInfo-content-row-li">
                <div class="dialog-content-BasicInfo-content-row-li-top">请求方法</div>
                <div class="dialog-content-BasicInfo-content-row-li-bottom">{{form.requestMethod}}</div>
              </div>

            </div>
            <div class="dialog-content-BasicInfo-content-row">
              <div class="dialog-content-BasicInfo-content-row-li">
                <div class="dialog-content-BasicInfo-content-row-li-top">请求URL</div>
                <div class="dialog-content-BasicInfo-content-row-li-bottom">{{form.operUrl}}</div>
              </div>
            </div>
          </div>
        </div>
        <div class="dialog-content-BasicInfo">
          <div class="dialog-content-BasicInfo-title">报文数据</div>
          <div class="dialog-content-BasicInfo-content" style="display: flex;">
            <div class="dialog-content-BasicInfo-content-li" style="margin-right: 16px">
              <div class="dialog-content-BasicInfo-content-li-title">请求报文</div>
              <div class="dialog-content-BasicInfo-content-li-content">{{form.operParam}}</div>
            </div>
            <div class="dialog-content-BasicInfo-content-li">
              <div class="dialog-content-BasicInfo-content-li-title">返回报文</div>
              <div class="dialog-content-BasicInfo-content-li-content">{{form.jsonResult}}</div>
            </div>
          </div>
        </div>
      </div>
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
<!--        <el-button @click="cancel">取 消</el-button>-->
<!--      </div>-->
    </el-dialog>
    <el-dialog title="文件上传" :visible.sync="open1" width="60%" append-to-body>
      <el-form ref="form1" :model="resourceFiles" :rules="rules1" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="资源名称" prop="resourceName">
              <el-input v-model="resourceFiles.resourceName" placeholder="请输入资源名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="资源详情" >
              <el-input v-model="resourceFiles.resourceDetails" placeholder="请输入资源详情" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="文件上传" prop="resourceUrl">
              <file-upload v-model="resourceFiles.resourceUrl" :file-type='["doc", "xls", "ppt", "txt", "pdf", "png", "jpg", "jpeg"]'/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDictType, getResource, delResource, addResource, updateResource } from "@/api/classai/resource";
import {apiLogList} from "@/api/system/operlog";
import {allListSchool} from "@/api/classai/school";

export default {
  name: "operateLog",
  dicts: ['sys_business_type', 'sys_operator_type'],
  data() {
    return {
      status:0,
      businessList:[],
      operatorList:[],
      // 日期范围
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资源库表格数据
      logList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessType: null,
        title: null,
        operatorType: null,
        schoolId: '',
        operName: null,
        startTime: null,
        endTime: null
      },
      schoolList: [],
      resourceFiles:{},
      // 表单参数
      form: {
        resourceFiles: [
          {
            resourceUrl:'',
            resourceName:'',
            resourceDetails:'',
          }
        ],
      },
      // 表单校验
      rules: {
        resourceName: [
          { required: true, message: "资源名称不能为空", trigger: "blur" }
        ],
      },
      // 表单校验
      rules1: {
        resourceName: [
          { required: true, message: "资源名称不能为空", trigger: "blur" }
        ],
        resourceUrl: [
          { required: true, message: "资源地址不能为空", trigger: "change" }
        ],
      }

    };
  },
  created() {
    // this.getBusinessList()
    // this.getOperatorList()
    this.getList();
    this.getSchoolData()
  },
  methods: {
    getSchoolData() {
      allListSchool().then(res => {
        this.schoolList = res.data
      })
    },
    // async getBusinessList() {
    //   getDictType({ dictType: "sys_business_type" }).then((res) => {
    //     // this.businessList = res.data.map((it) => {
    //     //   return { value: it.dictValue, label: it.dictLabel };
    //     // });
    //     console.log(res,'this.businessList1111')
    //   });
    // },
    // async getOperatorList() {
    //   getDictType({ dictType: "sys_operator_type" }).then((res) => {
    //     console.log(res,'----')
    //     // this.operatorList = res.data.map((it) => {
    //     //   return { value: it.dictValue, label: it.dictLabel };
    //     // });
    //   });
    // },
    // 打开选取文件弹窗
    openFile(){
      this.open1 = true
    },
    /** 查询资源库列表 */
    getList() {
      this.loading = true;
      apiLogList(this.queryParams).then(response => {
        this.logList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancel1() {
      this.open1 = false;
      this.reset1();
    },
    reset1(){
      this.resourceFiles = {
        resourceUrl:'',
        resourceName:'',
        resourceDetails:'',
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        resourceName: null,
        remarks: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        resourceImg: null,
        resourceFiles: null,
        browseNum: null,
        status: null,
        gradeType: null,
        subjectType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if(this.dateRange.length>0){
        this.queryParams.startTime = this.dateRange[0]
        this.queryParams.endTime = this.dateRange[1]
      }

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {}
      this.dateRange = []
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资源库";
    },
    /** 修改按钮操作 */
    handleDetails(row) {
      this.reset();
      this.form = row
      this.open = true;
      this.title = "操作日志详情";

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateResource(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResource(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 资源地址提交按钮 */
    submitForm1() {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          if (!this.form.resourceFiles) {
            this.form.resourceFiles = [this.resourceFiles]
          } else {
            this.form.resourceFiles.push(this.resourceFiles)
          }
          this.open1 = false
          this.reset1()
        }
      });
    },
    handleView(e) {
      window.open(e.resourceUrl, '_blank')
    },
    handleDelete1(e){
      const index = this.form.resourceFiles.findIndex(it => it === e)
      if (index < 0) { return }
      this.form.resourceFiles.splice(index, 1)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除资源库编号为"' + ids + '"的数据项？').then(function() {
        return delResource(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/apiLog/export', {
        ...this.queryParams
      }, `resource_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
.operateLog{
  width: 100%;
  height: 100%;
  background: #F9FAFB;
  padding: 24px;
  box-sizing: border-box;
  .operateLog-content{
    width: 100%;
    height: 100%;
    padding: 24px;
    box-sizing: border-box;
    border-radius: 12px;
    background-color: #ffffff;
    box-shadow: 0px 2px 4px -2px rgba(0, 0, 0, 0.1),0px 4px 6px -1px rgba(0, 0, 0, 0.1);
    overflow-y: auto;

    .operateLog-content-title{
      width: 100%;
      display: flex;
      justify-content: space-between;
      margin-bottom: 32px;
      .operateLog-content-title-left{
        font-size: 24px;
        font-weight: bold;
        color: #1F2937;
      }
    }
    .operateLog-content-search{
      width: 100%;
      padding: 24px;
      box-sizing: border-box;
      background-color: #F9FAFB;
      border-radius: 8px;
      margin-bottom: 32px;
      .operateLog-content-search-title{
        font-size: 18px;
        color: #374151;
        font-weight: 600;
        margin-bottom: 16px;
      }
      .operateLog-content-search-content{
        width: 100%;
        display: flex;
        align-items: flex-end;
        justify-content: space-between;
      }
    }
  }
}

::v-deep .el-form-item--small.el-form-item {
  margin-bottom: 0 !important;
  margin-right: 18px !important;
}
.dialog-content{
  width: 100%;
  .dialog-content-BasicInfo{
    width: 100%;
    padding: 16px;
    box-sizing: border-box;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    background: #F9FAFB;
    margin-bottom:24px;
    .dialog-content-BasicInfo-title{
      width: 100%;
      height: 37px;
      font-size: 18px;
      color: #111827;
      display: flex;
      align-items: center;
      border-bottom: 1px solid #E5E7EB;
      margin-bottom: 16px;
    }
    .dialog-content-BasicInfo-content{
      flex: 1;
      .dialog-content-BasicInfo-content-row{
        width: 100%;
        display: flex;
        justify-content: space-between;
        .dialog-content-BasicInfo-content-row-li{
          flex: 1;
          margin-bottom: 16px;
          .dialog-content-BasicInfo-content-row-li-top{
            font-size: 14px;
            font-weight: 500;
            line-height: 20px;
            color: #6B7280;
          }
          .dialog-content-BasicInfo-content-row-li-bottom{
            font-size: 14px;
            line-height: 20px;
            color: #111827;
            margin-top: 4px;

          }
        }
      }
      .dialog-content-BasicInfo-content-li{
        flex: 1;

        .dialog-content-BasicInfo-content-li-title{
          font-size: 16px;
          line-height: 24px;
          color: #374151;
          margin-bottom: 8px;
        }
        .dialog-content-BasicInfo-content-li-content{
          padding: 16px;
          box-sizing: border-box;
          border: 1px solid #E5E7EB;
          border-radius: 8px;
          background: #FFFFFF;
        }
      }
    }
  }
}
.status-button{
  font-size: 12px;
  font-weight: 600;
  width: 40px;
  height: 20px;
  padding: 0 8px;
  box-sizing: border-box;
  border-radius: 10px;
  background-color: red;
}
.status-color-fail{
  color: #991B1B;
  background: #FEE2E2;
}
.status-color-modify{
  color: #1E40AF;
  background: #DBEAFE;
}
.status-color-import{
  color: #1F2937;
  background: #F3F4F6;
}
.status-color-login{
  color: #6B21A8;
  background: #F3E8FF;
}
.status-color-add{
  color: #67c23a;
  background: #f0f9eb;
}

.status-color-logOut{
  color: #854D0E;
  background: #FEF9C3;
}
</style>
